//
//  macro.h
//  MOS
//
//  Created by yls on 14-5-4.
//  Copyright (c) 2014年 SITE. All rights reserved.
//
//  iOS 相关宏定义
//
#import <UIKit/UIKit.h>


#ifndef MOS_macro_h
#define MOS_macro_h

#ifdef __cplusplus
extern "C" {
#endif
#pragma mark - keyWindow
extern UIView* KeyWindow_view();
    
extern UINavigationController* RootNavigationController();

// 根视图控制器
#define ROOT_VIEWCONTROLLER [UIApplication sharedApplication].keyWindow.rootViewController
// 根视图控制器上的视图
#define ROOT_VIEW ROOT_VIEWCONTROLLER.view
    
#pragma mark - Device && Screen
/// 根据屏幕标准分辨率来确定当前的设备
#define iPhone5 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 1136), [[UIScreen mainScreen] currentMode].size) : NO)
    
#define iPhone6 ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(750, 1334), [[UIScreen mainScreen] currentMode].size) : NO)
    
#define iPhone6Plus ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(1242, 2208), [[UIScreen mainScreen] currentMode].size) : NO)
    
    
/// 是否iPad
#define isPad (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
/// 是否模拟器
#ifdef TARGET_IPHONE_SIMULATOR
#define isSimulator TARGET_IPHONE_SIMULATOR
#endif
    //Layer 的 描边颜色
#define BORDER_LAYER_COLOR [UIColor colorWithHex:0xcccccc].CGColor
    //Layer 的 描边颜色
#define BORDER_LAYER_WIDTRH 0.5
#define Screen_frame [UIScreen mainScreen].bounds
/// 屏幕高度、宽度
#define Screen_height [[UIScreen mainScreen] bounds].size.height
#define Screen_width [[UIScreen mainScreen] bounds].size.width
    
/// 以 4-inck 屏幕尺寸为基准的宽度（高度）比例
#define Ratio_Width_4 Screen_width/320.0
#define Ratio_Height_4 Screen_height/568.0
/// 以 4.7-inck 屏幕尺寸为基准的宽度（高度）比例
#define Ratio_Width_4_7 Screen_width/375.0
#define Ratio_Height_4_7 Screen_height/667.0
/// 以 5.5-inck 屏幕尺寸为基准的宽度（高度）比例
#define Ratio_Width_5_5 Screen_width/414.0
#define Ratio_Height_5_5 Screen_height/736.0
    
#pragma mark - System Version
/// 当前系统版本大于等于某版本
#define IOS_SYSTEM_VERSION_EQUAL_OR_ABOVE(v) (([[[UIDevice currentDevice] systemVersion] floatValue] >= (v))? (YES):(NO))
/// 当前系统版本小于等于某版本
#define IOS_SYSTEM_VERSION_EQUAL_OR_BELOW(v) (([[[UIDevice currentDevice] systemVersion] floatValue] <= (v))? (YES):(NO))
/// 当前系统版本
#define IOS_SYSTEM_VERSION [[[UIDevice currentDevice] systemVersion] floatValue]
#define iOS7 (IOS_SYSTEM_VERSION >= 7.0)
/// 系统语言
#define IOS_SYSTEM_Language ([[NSLocale preferredLanguages] objectAtIndex:0])
/// 当前应用版本号
//#define AppVersion [[NSBundle mainBundle] infoDictionary][@"CFBundleVersion"]
#pragma mark - common path
/// 常用文件路径
#define PathForDocument NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]
#define PathForLibrary NSSearchPathForDirectoriesInDomains (NSLibraryDirectory, NSUserDomainMask, YES)[0]
#define PathForCaches NSSearchPathForDirectoriesInDomains (NSCachesDirectory, NSUserDomainMask, YES)[0]
#pragma mark - shared tool
#define SharedUserDefault [NSUserDefaults standardUserDefaults]
#define SharedNotificationCenter [NSNotificationCenter defaultCenter]
#define SharedFileManager [NSFileManager defaultManager]
#define SharedApplicationDelegate [[UIApplication sharedApplication] delegate]
#pragma mark - image && color
/// 加载图片
#define UIImageLoad(name, type) [UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:name ofType:type]]
//#define UIImageNamed( name ) [UIImage imageNamed: name]
extern UIImage* UIImageNamed(NSString* name);

extern UIImage* UIImageWithColor(UIColor *color);

/// 颜色
//#define UIColorWithRGBHex(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
extern UIColor* UIColorWithRGBHex(NSInteger rgbValue);
//#define UIColorWithRGBA(r, g, b, a) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:a]
extern UIColor* UIColorWithRGBA(NSInteger r,NSInteger g,NSInteger b,NSInteger a);
//#define UIColorWithRGB(r, g, b) [UIColor colorWithRed:r/255.0f green:g/255.0f blue:b/255.0f alpha:1.0]
extern UIColor* UIColorWithRGB(NSInteger r,NSInteger g,NSInteger b);
    
#pragma mark - GCD
/** 全局线程执行方法 */
extern void st_dispatch_async_global(dispatch_block_t block);
/** 主线程执行方法 */
extern void st_dispatch_async_main(dispatch_block_t block);
/** 主线程延时执行方法 */
extern void st_dispatch_async_main_after(NSTimeInterval ti, dispatch_block_t block);


/** 根据给定字体计算单位高度 */
extern CGFloat UnitHeightOfFont(UIFont*font);

/** 根据字符串、最大尺寸、字体计算字符串最合适尺寸 */
extern CGSize CGSizeOfString(NSString * text, CGSize maxSize, UIFont * font);
/** 设置视图大小，原点不变 */
extern void SetViewSize(UIView *view, CGSize size);
/** 设置视图宽度，其他不变 */
extern void SetViewSizeWidth(UIView *view, CGFloat width);
/** 设置视图高度，其他不变 */
extern void SetViewSizeHeight(UIView *view, CGFloat height);
/** 设置视图原点，大小不变 */
extern void SetViewOrigin(UIView *view, CGPoint pt);
/** 设置视图原点x坐标，大小不变 */
extern void SetViewOriginX(UIView *view, CGFloat x);
/** 设置视图原点y坐标，大小不变 */
extern void SetViewOriginY(UIView *view, CGFloat y);

extern void setSubviewDelaysContentTouchesNO(UIView * v);

/** data format */

#define NSStringFromNumber_c( __v__ ) [NSString stringWithFormat:@"%@", @(__v__)]
#define NSStringFromObject_oc( __v__ ) [NSString stringWithFormat:@"%@", __v__]

/**
 *	@brief	json字符串 转换成 dic
 *
 *	@param 	jsonString 	json  字符串
 *
 *	@return	nsdictionary
 */
extern NSDictionary * dictionaryWithJsonString(NSString* jsonString);
extern NSArray * ArrayWithJsonString(NSString* jsonString);
    
#pragma mark - 可变参数格式化
extern NSString* StringWithFormat(NSString*format,...);
    
#pragma mark - 可变参数格式化 MD5
extern  NSString* StringWithFormatMD5(NSString*format,...);
    
/**
 *	@brief	只需要传入接口编号 和字典 就能返回 加密的字符串
 *
 *	@param 	numberStr 	接口编号
 *	@param 	dic 	请求参数
 *
 *	@return	md5 string
 */
extern NSString* StringWithFormatMD5JYC(NSString* numberStr,NSDictionary* dic);

/**
 *	@brief	设置 UIFont 的缩放比例
 *
 *	@param 	font 	原始 UIFont
 *	@param 	ratio 	缩放比例
 *
 *	@return	返回设置后的 UIFont
 */
extern UIFont * SetFontWithBaseFont(UIFont *font , float ratio);


/**
 *	@brief	设置App页面显示的badge
 *
 */
extern void ApplicationIconBadgeNumber(NSInteger num);

    
#pragma mark - 打开系统级应用
// 拨打电话
extern void ApplicationOpenTelWithPhoneNumber(NSString*phoneNum);
// 发短信
//extern void ApplicationOpenSMSWithPhoneNumber(NSString*phoneNum);
// 打开应用
extern void ApplicationOpenURLWithString(NSString*string);
    

#pragma mark - DEBUG
/** ======================= 调试相关宏定义 ========================== */
/// 添加调试控制台输出
#ifdef DEBUG
    
#define NSLogRect(__rect__) NSLog(@"x:%f , y:%f , w:%f , h:%f",__rect__.origin.x,__rect__.origin.y,__rect__.size.width,__rect__.size.height);
#define DDLog(fmt, ...) NSLog((@"%s [Line %d]\n😄 " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#define ELog(fmt, ...) NSLog((@"%s [Line %d]\n😥 " fmt), __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);
#else
#define NSLogRect(...)
#define DDLog(...)
#define ELog(...)
#define NSLog(...)
#define print(...)
#endif
/// 是否输出dealloc log
//#define Dealloc
#ifdef Dealloc
#define DeallocLog(fmt, ...) NSLog((fmt @"dealloc ..."), ##__VA_ARGS__);
#else
#define DeallocLog(...)
#endif

#pragma mark - singleton
//singleton
//
// SynthesizeSingleton.h
// CocoaWithLove
//
// Created by Matt Gallagher on 20/10/08.
// Copyright 2009 Matt Gallagher. All rights reserved.
//
// Permission is given to use this source code file without charge in any
// project, commercial or otherwise, entirely at your risk, with the condition
// that any redistribution (in part or whole) of source code must retain
// this copyright and permission notice. Attribution in compiled projects is
// appreciated but not required.
//
#define SYNTHESIZE_SINGLETON_FOR_CLASS(classname) \
\
static classname *shared##classname = nil; \
\
+ (classname *)shared##classname \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [[self alloc] init]; \
} \
} \
\
return shared##classname; \
} \
\
+ (id)allocWithZone:(NSZone *)zone \
{ \
@synchronized(self) \
{ \
if (shared##classname == nil) \
{ \
shared##classname = [super allocWithZone:zone]; \
return shared##classname; \
} \
} \
\
return nil; \
} \
\
- (id)copyWithZone:(NSZone *)zone \
{ \
return self; \
}

#ifdef __cplusplus
}
#endif
    
#endif
